﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine.Cameras;


namespace PloobsProjectTemplate
{
    class Maze : IVertexType
    {
        #region Fields

        public const int mazeWidth = 200;

        public const int mazeHeight = 200;

        GraphicsDevice device;

        VertexBuffer floorBuffer;

        private Random rand = new Random();

        //public MazeCell[,] mazeCell = new MazeCell[mazeWidth, mazeHeight];

        Color[] floorColors = new Color[2] { Color.White, Color.Black };

        Matrix viewMatrix, projectionMatrix;

        VertexPositionColor[] vert;

        List<VertexPositionColor> vertexList;

        #endregion

        #region Constructor
        public Maze(GraphicsDevice device)
        {
            this.device = device;
            BuildFloorBuffer();
            SetUpCamera();
            //for (int X = 0; X < mazeWidth; X++)

       }

       

        #endregion

        #region The Floor

        private void BuildFloorBuffer()
        {
            //vert = new VertexPositionColor[mazeWidth*mazeHeight*6];

           vertexList =
            new List<VertexPositionColor>();

            int counter = 0;
            for (int x = 0; x < mazeWidth; x++)
            {
                counter++;
                for (int z = 0; z < mazeHeight; z++)
                {
                    counter++;
                    foreach (VertexPositionColor vertex in
                    FloorTile(x, z, floorColors[counter % 2]))
                    {
                        //vert[counter] = vertex;
                        vertexList.Add(vertex);
                    }
                }
            }

            list2array();

            floorBuffer = new VertexBuffer(
            device,
            VertexPositionColor.VertexDeclaration,
            vertexList.Count,
            BufferUsage.WriteOnly);
            floorBuffer.SetData<VertexPositionColor>(vertexList.
            ToArray());
        }

        private List<VertexPositionColor> FloorTile(int xOffset,
                                                    int zOffset,
                                                    Color tileColor)
        {
            List<VertexPositionColor> vList = new List<VertexPositionColor>();
            vList.Add(new VertexPositionColor(
            new Vector3(0 + xOffset, 0, 0 + zOffset), tileColor));
            vList.Add(new VertexPositionColor(
            new Vector3(1 + xOffset, 0, 0 + zOffset), tileColor));
            vList.Add(new VertexPositionColor(
            new Vector3(0 + xOffset, 0, 1 + zOffset), tileColor));
            vList.Add(new VertexPositionColor(
            new Vector3(1 + xOffset, 0, 0 + zOffset), tileColor));
            vList.Add(new VertexPositionColor(
            new Vector3(1 + xOffset, 0, 1 + zOffset), tileColor));
            vList.Add(new VertexPositionColor(
            new Vector3(0 + xOffset, 0, 1 + zOffset), tileColor));
            return vList;
        }

        private void list2array()
        {
            vert = new VertexPositionColor[vertexList.Count];
            //foreach (VertexPositionColor i in vertexList)
            for (int i = 0; i < vertexList.Count; i++)
            {
                vert[i] = vertexList[i];
            }
            
        }

        #endregion

        #region Draw

        private void SetUpCamera()
        {
            viewMatrix = Matrix.CreateLookAt(new Vector3(3, 5, 2), new Vector3(2, 0, -1), new Vector3(0, 1, 0));
            projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 0.2f, 500.0f);
        }

        public void Draw(ICamera camera, BasicEffect effect)
        {
            effect.VertexColorEnabled = true;
            effect.World = Matrix.Identity;
            effect.View = viewMatrix;
            effect.Projection = projectionMatrix;

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                device.SetVertexBuffer(floorBuffer);
                device.DrawPrimitives(
                PrimitiveType.TriangleList,
                0,
                floorBuffer.VertexCount / 3);
            }
        }

        public void Draw(PloobsEngine.SceneControl.RenderHelper render, BasicEffect effect)
        {
            effect.VertexColorEnabled = true;
            effect.World = Matrix.Identity;
            effect.View = viewMatrix;
            effect.Projection = projectionMatrix;

            render.RenderUserPrimitive<VertexPositionColor>(effect, PrimitiveType.TriangleList, vert, 0, floorBuffer.VertexCount / 3);
        }

        //public void Draw(ICamera camera, BasicEffect effect)
        //{view
        //    effect.VertexColorEnabled = true;
        //    effect.World = Matrix.Identity;
        //    effect.View = camera.View;
        //    effect.Projection = camera.Projection;

        //    foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        //    {
        //        pass.Apply();
        //        device.SetVertexBuffer(floorBuffer);vert
        //        device.DrawPrimitives(
        //        PrimitiveType.TriangleList,
        //        0,
        //        floorBuffer.VertexCount / 3);
        //    }
        //}
        #endregion




        public VertexDeclaration VertexDeclaration
        {
            get { throw new NotImplementedException(); }
        }
    }
}
